#!/bin/sh


PKGTAR=lfs-packages-6.8.tar

# 1. Preparings
# 1.1 Create a new partition

#mke2fs -jv /dev/sdc1
#mkswap /dev/sdc2

#export LFS=/mnt/lfs
#mkdir -pv $LFS
#mount -v -t ext3 /dev/sdc1 $LFS
#/sbin/swapon -v /dev/sdc2

#mkdir -v $LFS/sources
#wget -i wget-list -P $LFS/sources
#mkdir -v $LFS/tools
#sudo ln -sv $LFS/tools

#sudo groupadd lfs
#sudo useradd -s /bin/bash -g lfs -m -k /dev/null lfs
#sudo passwd lfs

# 1.2 Create a new user and group
#sudo chown -v lfs $LFS/tools
#sudo chown -v lfs $LFS/sources
#su - lfs

#cat > ~/.bash_profile << "EOF"
#exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
#EOF

#cat > ~/.bashrc << "EOF"
#set +h
#umask 022
#LFS=/mnt/lfs
#LC_ALL=POSIX
#LFS_TGT=$(uname -m)-lfs-linux-gnu
#PATH=/tools/bin:/bin:/usr/bin
#export LFS LC_ALL LFS_TGT PATH
#EOF

#export MAKEFLAGS='-j 2'

# 1.3 Construction a Temporary System
# 5.4 Binutils-2.21 - Pass 1
step0504() {
    cd $LFS/sources

    if [ -d binutils-2.21 ]; then
        rm -rf binutils-2.21
    fi
    tar -xvjf binutils-2.21.tar.bz2 || exit -1

    if [ -d binutils-build ]; then
        rm -rf binutils-build
    fi
    mkdir -v binutils-build
    cd binutils-build
    
    ../binutils-2.21/configure \
        --target=$LFS_TGT --prefix=/tools \
        --disable-nls --disable-werror || exit -1
    make && make install || exit -1

    cd $LFS/sources
    rm -rf binutils-build
    rm -rf binutils-2.21
}

# 5.5 GCC-4.5.2 - Pass 1
step0504() {
    cd $LFS/sources
    if [ -d gcc-4.5.2 ]; then
        rm -rvf gcc-4.5.2
    fi
    tar -xvjf gcc-4.5.2.tar.bz2 || exit -1
    cd gcc-4.5.2

    tar -jxf ../mpfr-3.0.0.tar.bz2
    mv -v mpfr-3.0.0 mpfr
    tar -jxf ../gmp-5.0.1.tar.bz2
    mv -v gmp-5.0.1 gmp
    tar -zxf ../mpc-0.8.2.tar.gz
    mv -v mpc-0.8.2 mpc
    
    mkdir -v ../gcc-build || exit -1
    cd ../gcc-build
    
    ../gcc-4.5.2/configure \
        --target=$LFS_TGT --prefix=/tools \
        --disable-nls --disable-shared --disable-multilib \
        --disable-decimal-float --disable-threads \
        --disable-libmudflap --disable-libssp \
        --disable-libgomp --enable-languages=c \
        --with-gmp-include=$(pwd)/gmp --with-gmp-lib=$(pwd)/gmp/.libs \
        --without-ppl --without-cloog || exit -1
    make && make install || exit -1
    
    ln -vs libgcc.a `$LFS_TGT-gcc -print-libgcc-file-name | \
        sed 's/libgcc/&_eh/'` || exit -1
}

## 5.6 Linux-2.6.37 API Headers
step0506() {
    cd $LFS/sources
    if [ -d linux-2.6.37 ]; then
        rm -rvf linux-2.6.37
    fi
    tar -xvjf linux-2.6.37.tar.bz2 || exit -1
    cd linux-2.6.37

    make mrproper && make headers_check \
    && make INSTALL_HDR_PATH=dest headers_install || exit -1
    cp -rv dest/include/* /tools/include 
}

# 5.7 Glibc-2.13
step0507() {
    cd $LFS/sources
    if [ -d glibc-2.13 ]; then
        rm -rvf glibc-2.13
    fi
    tar -xvjf glibc-2.13.tar.bz2 || exit -1
    cd glibc-2.13

    patch -Np1 -i ../glibc-2.13-gcc_fix-1.patch
    
    mkdir -v ../glibc-build
    cd ../glibc-build
    
    case `uname -m` in
    i?86) echo "CFLAGS += -march=i486 -mtune=native" > configparms ;;
    esac
    
    ../glibc-2.13/configure --prefix=/tools \
        --host=$LFS_TGT --build=$(../glibc-2.13/scripts/config.guess) \
        --disable-profile --enable-add-ons \
        --enable-kernel=2.6.22.5 --with-headers=/tools/include \
        libc_cv_forced_unwind=yes libc_cv_c_cleanup=yes || exit -1
    make && make install || exit -1

    cd $LFS/sources
    rm -rf glibc-build
    rm -rf glibc-2.13
}

## 5.8 Adjusting the Toolchain
step0508() {
    cd $LFS/sources
    SPECS=`dirname $($LFS_TGT-gcc -print-libgcc-file-name)`/specs
    $LFS_TGT-gcc -dumpspecs | sed \
        -e 's@/lib\(64\)\?/ld@/tools&@g' \
        -e "/^\*cpp:$/{n;s,$, -isystem /tools/include,}" > $SPECS || exit -1
    echo "New specs file is: $SPECS"
    unset SPECS
    
    # Sanity check
    cd $LFS
    echo 'main(){}' > dummy.c
    $LFS_TGT-gcc -B/tools/lib dummy.c
    # Expected:
    # [Requesting program interpreter: /tools/lib/ld-linux.so.2]
    readelf -l a.out | grep ': /tools' || exit -1

    rm -v dummy.c a.out
}

## 5.9 Binutils-2.21 - Pass 2
step0509() {
    cd $LFS/sources

    if [ -d binutils-2.21 ]; then
        rm binutils-2.21
    fi
    tar -xvjf binutils-2.21.tar.bz2 || exit -1

    if [ -d binutils-build ]; then
        rm binutils-build
    fi
    mkdir -v binutils-build

    cd binutils-build
    
    CC="$LFS_TGT-gcc -B/tools/lib/" \
    AR=$LFS_TGT-ar RANLIB=$LFS_TGT-ranlib \
    ../binutils-2.21/configure --prefix=/tools \
       --disable-nls --with-lib-path=/tools/lib || exit -1
    make && make install \
    make -C ld clean \
    make -C ld LIB_PATH=/usr/lib:/lib || exit -1
    cp -v ld/ld-new /tools/bin 

    cd $LFS/sources
    rm -rf binutils-build
    rm -rf binutils-2.21
}

## 5.10 GCC-4.5.2 - Pass 2
step0510 () {
    cd $LFS/sources
    if [ -d gcc-4.5.2 ]; then
        rm -rvf gcc-4.5.2
    fi
    tar -xvjf gcc-4.5.2.tar.bz2 || exit -1
    cd gcc-4.5.2
    
    patch -Np1 -i ../gcc-4.5.2-startfiles_fix-1.patch
    cp -v gcc/Makefile.in{,.orig}
    sed 's@\./fixinc\.sh@-c true@' gcc/Makefile.in.orig > gcc/Makefile.in
    
    cp -v gcc/Makefile.in{,.tmp}
    sed 's/^T_CFLAGS =$/& -fomit-frame-pointer/' gcc/Makefile.in.tmp \
        > gcc/Makefile.in
    
    for file in \
        $(find gcc/config -name linux64.h -o -name linux.h -o -name sysv4.h)
    do
        cp -uv $file{,.orig}
        sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&@g' \
            -e 's@/usr@/tools@g' $file.orig > $file
        echo '
#undef STANDARD_INCLUDE_DIR
#define STANDARD_INCLUDE_DIR 0
#define STANDARD_STARTFILE_PREFIX_1 ""
#define STANDARD_STARTFILE_PREFIX_2 ""' >> $file
        touch $file.orig
    done || exit -1

    case $(uname -m) in
    x86_64)
    for file in $(find gcc/config -name t-linux64) ; do \
        cp -v $file{,.orig}
        sed '/MULTILIB_OSDIRNAMES/d' $file.orig > $file
    done
    ;;
    esac
    tar -jxf ../mpfr-3.0.0.tar.bz2
    mv -v mpfr-3.0.0 mpfr
    tar -jxf ../gmp-5.0.1.tar.bz2
    mv -v gmp-5.0.1 gmp
    tar -zxf ../mpc-0.8.2.tar.gz
    mv -v mpc-0.8.2 mpc
    
    if [ -d gcc-build ]; then
        rm -rvf gcc-build
    fi
    mkdir -v ../gcc-build || exit -1
    cd ../gcc-build
    
    CC="$LFS_TGT-gcc -B/tools/lib/" \
    AR=$LFS_TGT-ar RANLIB=$LFS_TGT-ranlib \
    ../gcc-4.5.2/configure --prefix=/tools \
    --with-local-prefix=/tools --enable-clocale=gnu \
    --enable-shared --enable-threads=posix \
    --enable-__cxa_atexit --enable-languages=c,c++ \
    --disable-libstdcxx-pch --disable-multilib \
    --disable-bootstrap --disable-libgomp \
    --with-gmp-include=$(pwd)/gmp --with-gmp-lib=$(pwd)/gmp/.libs \
    --without-ppl --without-cloog || exit -1
    make && make install || exit -1
    ln -vs gcc /tools/bin/cc

    # Sanity check
    cd $LFS/sources
    echo 'main(){}' > dummy.c
    cc dummy.c || exit -1
    readelf -l a.out | grep ': /tools' || exit -1
    # Expected:
    # [Requesting program interpreter: /tools/lib/ld-linux.so.2]
    rm -v dummy.c a.out
}

# 5.11 Tcl-8.5.9
step0511 () {
    cd $LFS/sources/
    if [ -d tcl8.5.9 ];then
        rm -rf tcl8.5.9
    fi
    tar -xzf tcl8.5.9.tar.gz
    cd tcl8.5.9
    cd unix
    ./configure --prefix=/tools || exit -1
    make && TZ=UTC make test && make install || exit -1
    chmod -v u+w /tools/lib/libtcl8.5.so
    make install-private-headers || exit -1
    ln -sv tclsh8.5 /tools/bin/tclsh

    cd $LFS/sources/
    rm -rf tcl-8.5.9
}

# 5.12 Expect-5.45
step0512() {
    cd $LFS/sources/
    #if [ -d expect5.45 ];then
    #    rm -rf expect5.45
    #fi
    #tar -xzf expect5.45.tar.gz
    cd expect5.45

    cp -v configure{,.orig}
    sed 's:/usr/local/bin:/bin:' configure.orig > configure
    
    ./configure --prefix=/tools --with-tcl=/tools/lib \
        --with-tclinclude=/tools/include || exit -1
    make && make test && make SCRIPTS="" install || exit -1

    cd $LFS/sources/
    rm -rf expect5.45
}

# 5.13 DejaGNU-1.4.4
step0513() {
    cd $LFS/sources/dejagnu-1.4.4 || exit -1

    patch -Np1 -i ../dejagnu-1.4.4-consolidated-1.patch
    ./configure --prefix=/tools || exit -1
    make install && make check || exit -1

    cd $LFS/sources/
    rm -rf dejagnu-1.4.4
}

# 5.14 Ncurses-5.7
step0514() {
    cd $LFS/sources/ncurses-5.7 || exit -1
    ./configure --prefix=/tools --with-shared \
        --without-debug --without-ada --enable-overwrite || exit -1
    make && make install || exit -1

    cd $LFS/sources/
    rm -rf ncurses-5.7
}

# 5.15 Bash-4.2
step0515() {
    cd $LFS/sources/bash-4.2 || exit -1
    
    ./configure --prefix=/tools --without-bash-malloc || exit -1
    make && make test && make install || exit -1
    ln -vs bash /tools/bin/sh
}

# 5.16 Bzip2-1.0.6
step0516() {
    cd $LFS/sources/bzip2-1.0.6

    make && make PREFIX=/tools install || exit -1
}

# 5.17 Coreutils-8.10
step0517() {
    cd $LFS/sources/
    if [ -d coreutils-8.10 ]; then
        rm -rf coreutils-8.10
    fi
    tar -xzf coreutils-8.10.tar.gz || exit -1
    cd coreutils-8.10
    
    ./configure --prefix=/tools --enable-install-program=hostname || exit -1
    # == check failed here!!!
    make && make RUN_EXPENSIVE_TESTS=yes check && make install || exit -1
    
    cp -v src/su /tools/bin/su-tools
}

# 5.18 Diffutils-3.0
step0518() {
    cd $LFS/sources/diffutils-3.0

    ./configure --prefix=/tools || exit -1
    make && make check && make install || exit -1
}

# 5.19 File-5.05
step0519() {
    cd $LFS/sources/file-5.05 || exit -1

    ./configure --prefix=/tools || exit -1
    # build zlib-1.2.5 first and copy libz.so.1.2.5 to /tools/lib/ and 
    # make a symbol link
    make && make check && make install || exit -1
}

# 5.20 Findutils-4.4.2
step0520() {
    cd $LFS/sources/findutils-4.4.2 || exit -1

    ./configure --prefix=/tools || exit -1
    # check failed
    make && make check && make install || exit -1
}

# 5.21 Gawk-3.1.8
step0521() {
    cd $LFS/sources/gawk-3.1.8 || exit -1

    ./configure --prefix=/tools || exit -1
    make && make check && make install || exit -1
}

# 5.22 Gettext-0.18.1.1
step0522() {
    cd $LFS/sources/gettext-0.18.1.1 || exit -1
    cd gettext-tools
    
    ./configure --prefix=/tools --disable-shared || exit -1
    make -C gnulib-lib || exit -1
    make -C src msgfmt || exit -1

    cp -v src/msgfmt /tools/bin
}

# 5.23 Grep-2.7
step0523() {
    cd $LFS/sources/grep-2.7

    ./configure --prefix=/tools --disable-perl-regexp || exit -1
    make && make check && make install || exit -1
}

# 5.24 Gzip-1.4
step0524() {
    cd $LFS/sources/gzip-1.4

    ./configure --prefix=/tools || exit -1
    make && make check && make install || exit -1
}

# 5.25 M4-1.4.15
step0525() {
    cd $LFS/sources/m4-1.4.15

    ./configure --prefix=/tools || exit -1
    make && make check && make install || exit -1
}

# 5.26 Make-3.82
step0526() {
    cd $LFS/sources/make-3.82 || exit -1

    ./configure --prefix=/tools || exit -1
    make && make check && make install || exit -1
}

# 5.27 Patch-2.6.1
step0527() {
    cd $LFS/sources/patch-2.6.1 || exit -1

    ./configure --prefix=/tools || exit -1
    make && make check && make install || exit -1
}

# 5.28 Perl-5.12.3
step0528() {
    cd $LFS/sources/perl-5.12.3

    patch -Np1 -i ../perl-5.12.3-libc-1.patch

    sh Configure -des -Dprefix=/tools \
        -Dstatic_ext='Data/Dumper Fcntl IO' || exit -1
    make perl utilities ext/Errno/pm_to_blib || exit -1

    cp -v perl pod/pod2man /tools/bin
    mkdir -pv /tools/lib/perl5/5.12.3 || exit -1
    cp -Rv lib/* /tools/lib/perl5/5.12.3
}

# 5.29 Sed-4.2.1
step0529() {
    cd $LFS/sources/sed-4.2.1

    ./configure --prefix=/tools || exit -1
    make && make check && make install || exit -1
}

# 5.30 Tar-1.25
step0530() {
    cd $LFS/sources/tar-1.25

    ./configure --prefix=/tools || exit -1
    make && make check && make install || exit -1
}

# 5.31 Texinfo-4.13a
step0531() {
    cd $LFS/sources/texinfo-4.13 || exit -1

    ./configure --prefix=/tools || exit -1
    make && make check && make install || exit -1
}

# 5.32 Xz-5.0.1
step0532() {
    cd $LFS/sources/xz-5.0.1

    ./configure --prefix=/tools || exit -1
    make && make check && make install || exit -1
}

# 5.33 Stripping
step0533() {
    cd $LFS/sources

    strip --strip-debug /tools/lib/*
    strip --strip-unneeded /tools/{,s}bin/*
    rm -rf /tools/{,share}/{info,man}
}

# 5.34 Changing Ownership
step0534() {
    sudo chown -R root:root $LFS/tools
    
    # Backup $LFS/tools
    cd $LFS
    
    tar cvjf lfs-tools.tar.bz2 $LFS/tools
}

# 6.2 Preparing Virtual Kernel File Systems
step0602() {
    mkdir -v $LFS/{dev,proc,sys}
    su
    mknod -m 600 $LFS/dev/console c 5 1
    mknod -m 666 $LFS/dev/null c 1 3
    mount -v --bind /dev/ $LFS/dev
    # Mounting Virtual Kernel File Systems
    mount -vt devpts devpts $LFS/dev/pts
    mount -vt tmpfs shm $LFS/dev/shm
    mount -vt proc proc $LFS/proc
    mount -vt sysfs sysfs $LFS/sys
}

# 6.4 Entering the Chroot Environment
step0604() {
    su
    chroot "$LFS" /tools/bin/env -i \
        HOME=/root TERM="$TERM" PS1='\u:\w\$ ' \
        PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
        /tools/bin/bash --login +h
}

step0605() {
    mkdir -pv /{bin,boot,etc/opt,home,lib,mnt,opt}
    mkdir -pv /{media/{floppy,cdrom},sbin,srv,var}
    install -dv -m 0750 /root
    install -dv -m 1777 /tmp /var/tmp
    mkdir -pv /usr/{,local/}{bin,include,lib,sbin,src}
    mkdir -pv /usr/{,local/}share/{doc,info,locale,man}
    mkdir -v /usr/{,local/}share/{misc,terminfo,zoneinfo}
    mkdir -pv /usr/{,local/}share/man/man{1..8}
    for dir in /usr /usr/local; do
        ln -sv share/{man,doc,info} $dir
    done
    case $(uname -m) in
    x86_64) ln -sv lib /lib64 && ln -sv lib /usr/lib64 ;;
    esac
    mkdir -v /var/{lock,log,mail,run,spool}
    mkdir -pv /var/{opt,cache,lib/{misc,locate},local}
}

# 6.6 Creating Essential Files and Symlinks
step0606() {
    ln -sv /tools/bin/{bash,cat,echo,pwd,stty} /bin
    ln -sv /tools/bin/perl /usr/bin
    ln -sv /tools/lib/libgcc_s.so{,.1} /usr/lib
    ln -sv /tools/lib/libstdc++.so{,.6} /usr/lib
    ln -sv bash /bin/sh

    touch /etc/mtab
    cat > /etc/passwd << "EOF"
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/dev/null:/bin/false
nobody:x:99:99:Unprivileged User:/dev/null:/bin/false
EOF
    cat > /etc/group << "EOF"
root:x:0:
bin:x:1:
sys:x:2:
kmem:x:3:
tty:x:4:
tape:x:5:
daemon:x:6:
floppy:x:7:
disk:x:8:
lp:x:9:
dialout:x:10:
audio:x:11:
video:x:12:
utmp:x:13:
usb:x:14:
cdrom:x:15:
mail:x:34:
nogroup:x:99:
EOF
    exec /tools/bin/bash --login +h
    touch /var/run/utmp /var/log/{btmp,lastlog,wtmp}
    chgrp -v utmp /var/run/utmp /var/log/lastlog
    chmod -v 664 /var/run/utmp /var/log/lastlog
}

# 6.7 Linux-2.6.37 API Headers
step0607() {
    cd $LFS/sources/linux-2.6.37 || exit -1

    make mrproper && make headers_check || exit -1
    make INSTALL_HDR_PATH=dest headers_install || exit -1
    find dest/include \( -name .install -o -name ..install.cmd \) -delete || exit -1
    cp -rv dest/include/* /usr/include
}

# 6.9 Man-pages-3.32
step0608() {
    cd $LFS/sources/man-pages-3.32 || exit -1
    make install || exit -1
}

# 6.9. Glibc-2.13
step0609() {
    cd /sources
    #if [ -d glibc-2.13 ]; then
    #    rm -rf glibc-2.13
    #fi
    #tar -xjf glibc-2.13.tar.bz2 || exit -1
    cd glibc-2.13

    DL=$(readelf -l /bin/sh | sed -n 's@.*interpret.*/tools\(.*\)]$@\1@p')
    sed -i "s|libs -o|libs -L/usr/lib -Wl,-dynamic-linker=$DL -o|" \
        scripts/test-installation.pl
    unset DL
    sed -i -e 's/"db1"/& \&\& $name ne "nss_test1"/' scripts/test-installation.pl
    sed -i 's|@BASH@|/bin/bash|' elf/ldd.bash.in
    patch -Np1 -i ../glibc-2.13-gcc_fix-1.patch || exit -1
    sed -i '195,213 s/PRIVATE_FUTEX/FUTEX_CLOCK_REALTIME/' \
        nptl/sysdeps/unix/sysv/linux/x86_64/pthread_rwlock_timed{rd,wr}lock.S || exit -1

    cd /sources
    if [ -d glibc-build ]; then
        rm -rf glibc-build
    fi
    mkdir -v glibc-build || exit -1
    cd glibc-build

    case `uname -m` in
    i?86) echo "CFLAGS += -march=i486 -mtune=native -O3 -pipe" > configparms ;;
    esac
    ../glibc-2.13/configure --prefix=/usr \
        --disable-profile --enable-add-ons \
        --enable-kernel=2.6.22.5 --libexecdir=/usr/lib/glibc || exit -1
    make || exit -1

    cp -v ../glibc-2.13/iconvdata/gconv-modules iconvdata
    # ==Critical Glibc==
    make -k check 2>&1 | tee glibc-check-log || exit -1
    grep Error glibc-check-log || exit -1

    touch /etc/ld.so.conf
    make install || exit -1

    mkdir -pv /usr/lib/locale || exit -1
    localedef -i cs_CZ -f UTF-8 cs_CZ.UTF-8
    localedef -i de_DE -f ISO-8859-1 de_DE
    localedef -i de_DE@euro -f ISO-8859-15 de_DE@euro
    localedef -i de_DE -f UTF-8 de_DE.UTF-8
    localedef -i en_HK -f ISO-8859-1 en_HK
    localedef -i en_PH -f ISO-8859-1 en_PH
    localedef -i en_US -f ISO-8859-1 en_US
    localedef -i en_US -f UTF-8 en_US.UTF-8
    localedef -i es_MX -f ISO-8859-1 es_MX
    localedef -i fa_IR -f UTF-8 fa_IR
    localedef -i fr_FR -f ISO-8859-1 fr_FR
    localedef -i fr_FR@euro -f ISO-8859-15 fr_FR@euro
    localedef -i fr_FR -f UTF-8 fr_FR.UTF-8
    localedef -i it_IT -f ISO-8859-1 it_IT
    localedef -i ja_JP -f EUC-JP ja_JP
    localedef -i tr_TR -f UTF-8 tr_TR.UTF-8
    localedef -i zh_CN -f GB18030 zh_CN.GB18030

    make localedata/install-locales || exit -1

    cat > /etc/nsswitch.conf << "EOF"
# Begin /etc/nsswitch.conf
passwd: files
group: files
shadow: files
hosts: files dns
networks: files
protocols: files
services: files
ethers: files
rpc: files
# End /etc/nsswitch.conf
EOF

    cp -v --remove-destination /usr/share/zoneinfo/Asia/Shanghai \
        /etc/localtime
    cat > /etc/ld.so.conf << "EOF"
# Begin /etc/ld.so.conf
/usr/local/lib
/opt/lib
# End /etc/ld.so.conf
EOF
}

step0610() {
    cd $LFS/sources

    mv -v /tools/bin/{ld,ld-old}
    mv -v /tools/$(gcc -dumpmachine)/bin/{ld,ld-old}
    mv -v /tools/bin/{ld-new,ld}
    ln -sv /tools/bin/ld /tools/$(gcc -dumpmachine)/bin/ld
    
    gcc -dumpspecs | sed -e 's@/tools@@g' \
        -e '/\*startfile_prefix_spec:/{n;s@.*@/usr/lib/ @}' \
        -e '/\*cpp:/{n;s@$@ -isystem /usr/include@}' > \
        `dirname $(gcc --print-libgcc-file-name)`/specs || exit -1
    
    echo 'main(){}' > dummy.c
    cc dummy.c -v -Wl,--verbose &> dummy.log
    readelf -l a.out | grep ': /lib' || exit -1

    grep -o '/usr/lib.*/crt[1in].*succeeded' dummy.log || exit -1
    grep -B1 '^ /usr/include' dummy.log || exit -1
    grep 'SEARCH.*/usr/lib' dummy.log |sed 's|; |\n|g'
    grep found dummy.log

    rm -v dummy.c a.out dummy.log
}

# 6.11 Zlib-1.2.5
step0611() {
    cd $LFS/sources
    cd zlib-1.2.5 || exit -1

    sed -i 's/ifdef _LARGEFILE64_SOURCE/ifndef _LARGEFILE64_SOURCE/' zlib.h
    CFLAGS='-mstackrealign -fPIC -O3' ./configure --prefix=/usr
    make && make check && make install || exit -1
    mv -v /usr/lib/libz.so.* /lib
    ln -sfv ../../lib/libz.so.1.2.5 /usr/lib/libz.so
}

# 6.12 Binutils-2.21
step0612() {
    cd $LFS/sources

    expect -c "spawn ls"
    rm -fv etc/standards.info
    sed -i.bak '/^INFO/s/standards.info //' etc/Makefile.in

    if [ -d binutils-2.21 ]; then
        rm -rf binutils-2.21
    fi
    tar -xjf binutils-2.21.tar.bz2 || exit -1

    if [ -d binutils-build ]; then
        rm -rf binutils-build
    fi
    mkdir -v binutils-build
    cd binutils-build

    ../binutils-2.21/configure --prefix=/usr --enable-shared || exit -1
    make tooldir=/usr || exit -1
    make check && make tooldir=/usr || exit -1

    cp -v ../binutils-2.21/include/libiberty.h /usr/include

    cd $LFS/sources
    rm -rf binutils-build
    rm -rf binutils-2.21
}

# 6.13 GMP-5.0.1
step0613() {
    cd $LFS/sources
    if [ -d gmp-5.0.1 ]; then
        rm -rf gmp-5.0.1
    fi
    tar -xjf gmp-5.0.1.tar.bz2 || exit -1
    cd gmp-5.0.1

    ABI=32 ./configure --prefix=/usr --enable-cxx --enable-mpbsd || exit -1
    make && make check 2>&1 | tee gmp-check-log || exit -1
    awk '/tests passed/{total+=$2} ; END{print total}' gmp-check-log
    make install || exit -1
    mkdir -v /usr/share/doc/gmp-5.0.1
    cp -v doc/{isa_abi_headache,configuration} doc/*.html /usr/share/doc/gmp-5.0.1
}

# MPFR-3.0.0
step0614() {
    cd $LFS/sources
    if [ -d mpfr-3.0.0 ]; then
        rm -rf mpfr-3.0.0
    fi
    tar -xjf mpfr-3.0.0.tar.bz2 || exit -1
    cd mpfr-3.0.0

    ./configure --prefix=/usr --enable-thread-safe \
        --docdir=/usr/share/doc/mpfr-3.0.0
    make && make install && make html && make install-html || exit -1
}

# 6.15 MPC-0.8.2
step0615() {
    cd $LFS/sources
    if [ -d mpc-0.8.2 ]; then
        rm -rf mpc-0.8.2
    fi
    tar -xzf mpc-0.8.2.tar.gz || exit -1
    cd mpc-0.8.2

    ./configure --prefix=/usr
    make && make check && make install || exit -1
}

step0616() {
    cd $LFS/sources

    sed -i 's/install_to_$(INSTALL_DEST) //' libiberty/Makefile.in
    case `uname -m` in
    i?86) sed -i 's/^T_CFLAGS =$/& -fomit-frame-pointer/' gcc/Makefile.in ;;
    esac
    sed -i 's@\./fixinc\.sh@-c true@' gcc/Makefile.in
    
    if [ -d gcc-build ]; then
        mkdir -v gcc-build
    fi
    cd gcc-build

    ../gcc-4.5.2/configure --prefix=/usr \
        --libexecdir=/usr/lib --enable-shared \
        --enable-threads=posix --enable-__cxa_atexit \
        --enable-clocale=gnu --enable-languages=c,c++ \
        --disable-multilib --disable-bootstrap --with-system-zlib
    make || exit -1
    ulimit -s 16384
    make -k check || exit -1
    ../gcc-4.5.2/contrib/test_summary
    make install || exit -1
    ln -sv ../usr/bin/cpp /lib
    ln -sv gcc /usr/bin/cc

    cd $LFS/sources
    echo 'main(){}' > dummy.c
    cc dummy.c -v -Wl,--verbose &> dummy.log || exit -1
    readelf -l a.out | grep ': /lib' || exit -1
    
    grep -o '/usr/lib.*/crt[1in].*succeeded' dummy.log || exit -1
    grep -B4 '^ /usr/include' dummy.log || exit -1
    grep 'SEARCH.*/usr/lib' dummy.log |sed 's|; |\n|g' || exit -1
    grep "/lib.*/libc.so.6 " dummy.log || exit -1
    grep found dummy.log || exit -1
    
    rm -v dummy.c a.out dummy.log
}

# 6.17 Sed-4.2.1
step0617() {
    cd $LFS/sources
    cd sed-4.2.1 || exit -1
    ./configure --prefix=/usr --bindir=/bin --htmldir=/usr/share/doc/sed-4.2.1 || exit -1
    make && make html && make check \
    && make install && make -c doc install-html || exit -1
}

# 6.18 Pkg-config-0.25
step0618() {
    cd $LFS/sources
    cd pkg-config-0.25 || exit -1
    ./configure --prefix=/usr || exit -1
    make && make check && make install || exit -1
}

# 6.19 Ncurses-5.7 
step0619() {
    cd $LFS/sources
    cd ncurses-5.7 || exit -1

    ./configure --prefix=/usr --with-shared --without-debug --enable-widec || exit -1
    make && make install || exit -1

    mv -v /usr/lib/libncursesw.so.5* /lib
    ln -sfv ../../lib/libncursesw.so.5 /usr/lib/libncursesw.so
    
    for lib in ncurses form panel menu ; do \
        rm -vf /usr/lib/lib${lib}.so ; \
        echo "INPUT(-l${lib}w)" >/usr/lib/lib${lib}.so ; \
        ln -sfv lib${lib}w.a /usr/lib/lib${lib}.a ; \
    done
    ln -sfv libncurses++w.a /usr/lib/libncurses++.a

    rm -vf /usr/lib/libcursesw.so
    echo "INPUT(-lncursesw)" >/usr/lib/libcursesw.so
    ln -sfv libncurses.so /usr/lib/libcurses.so
    ln -sfv libncursesw.a /usr/lib/libcursesw.a
    ln -sfv libncurses.a /usr/lib/libcurses.a
    
    mkdir -v /usr/share/doc/ncurses-5.7
    cp -v -R doc/* /usr/share/doc/ncurses-5.7

}

# 6.20 Util-linux-2.19
step0620() {
    cd $LFS/sources
    #tar -xjf util-linux-2.19.tar.bz2
    cd util-linux-2.19 || exit -1

    sed -e 's@etc/adjtime@var/lib/hwclock/adjtime@g' \
        -i $(grep -rl '/etc/adjtime' .)
    mkdir -pv /var/lib/hwclock

    ./configure --enable-arch --enable-partx --enable-write || exit -1
    make && make install || exit -1
}

# 6.21 E2fsprogs-1.41.14
step0621() {
    cd $LFS/sources
    cd e2fsprogs-1.41.14 || exit -1
    if [ -d build ]; then
        rm -rf build
    fi
    mkdir -v build
    cd build

    ../configure --prefix=/usr --with-root-prefix="" \
        --enable-elf-shlibs --disable-libblkid --disable-libuuid \
        --disable-uuidd --disable-fsck
    make && make check && make install && make install-libs || exit -1

    chmod -v u+w /usr/lib/{libcom_err,libe2p,libext2fs,libss}.a
    gunzip -v /usr/share/info/libext2fs.info.gz
    install-info --dir-file=/usr/share/info/dir /usr/share/info/libext2fs.info
    makeinfo -o doc/com_err.info ../lib/et/com_err.texinfo
    install -v -m644 doc/com_err.info /usr/share/info
    install-info --dir-file=/usr/share/info/dir /usr/share/info/com_err.info
}

# 6.22 Coreutils-8.10
step0622() {
    cd $LFS/sources
    cd coreutils-8.10 || exit -1

    case `uname -m` in
    i?86 | x86_64) patch -Np1 -i ../coreutils-8.10-uname-1.patch ;;
    esac
    patch -Np1 -i ../coreutils-8.10-i18n-1.patch

    ./configure --prefix=/usr \
        --enable-no-install-program=kill,uptime || exit -1
    make && make NON_ROOT_USERNAME=nobody check-root || exit -1

    echo "dummy:x:1000:nobody" >> /etc/group
    chown -Rv nobody .
    su-tools nobody -s /bin/bash -c "make RUN_EXPENSIVE_TESTS=yes check"
    sed -i '/dummy/d' /etc/group
    make install || exit -1

    mv -v /usr/bin/{cat,chgrp,chmod,chown,cp,date,dd,df,echo} /bin
    mv -v /usr/bin/{false,ln,ls,mkdir,mknod,mv,pwd,rm} /bin
    mv -v /usr/bin/{rmdir,stty,sync,true,uname} /bin
    mv -v /usr/bin/chroot /usr/sbin
    mv -v /usr/share/man/man1/chroot.1 /usr/share/man/man8/chroot.8
    sed -i s/\"1\"/\"8\"/1 /usr/share/man/man8/chroot.8
    mv -v /usr/bin/{head,sleep,nice} /bin
}

# 6.23 Iana-Etc-2.30
step0623() {
    cd $LFS/sources
    cd lana-etc-2.30 || exit -1
    make && make install || exit -1
}


# 6.24 M4-1.4.15
step0624() {
    cd $LFS/sources
    cd m4-1.4.15 || exit -1

    ./configure --prefix=/usr || exit -1
    make && make check && make install || exit -1
}

# 6.25 Bison-2.4.3
step0625() {
    cd $LFS/sources
    cd bison-2.4.3 || exit -1

    ./configure --prefix=/usr || exit -1
    echo '#define YYENABLE_NLS 1' >> lib/config.h
    make && make check && make install || exit -1
}

# 6.26 Procps-3.2.8
step0626() {
    cd $LFS/sources
    cd procps-3.2.8 || exit -1

    patch -Np1 -i ../procps-3.2.8-fix_HZ_errors-1.patch
    patch -Np1 -i ../procps-3.2.8-watch_unicode-1.patch
    sed -i -e 's@\*/module.mk@proc/module.mk ps/module.mk@' Makefile || exit -1
    make && make install || exit -1
}

# 6.27 Grep-2.7
step0627() {
    cd $LFS/sources
    cd grep-2.7 || exit -1

    ./configure --prefix=/usr --bindir=/bin || exit -1
    make && make check && make install || exit -1
}

# 6.28 Readline-6.2
step0628() {
    cd $LFS/sources
    cd readline-6.2 || exit -1

    sed -i '/MV.*old/d' Makefile.in
    sed -i '/{OLDSUFF}/c:' support/shlib-install
    ./configure --prefix=/usr --libdir=/lib || exit -1
    make SHLIB_LIBS=-lncurses && make install || exit -1

    mv -v /lib/lib{readline,history}.a /usr/lib
    rm -v /lib/lib{readline,history}.so
    ln -sfv ../../lib/libreadline.so.6 /usr/lib/libreadline.so
    ln -sfv ../../lib/libhistory.so.6 /usr/lib/libhistory.so
    mkdir -v /usr/share/doc/readline-6.2
    install -v -m644 doc/*.{ps,pdf,html,dvi} /usr/share/doc/readline-6.2
}

# 6.29 Bash-4.2
step0629() {
    cd $LFS/sources
    cd bash-4.2 || exit -1

    ./configure --prefix=/usr --bindir=/bin \
        --htmldir=/usr/share/doc/bash-4.2 --without-bash-malloc \
        --with-installed-readline || exit -1
    make || exit -1
    chown -Rv nobody .
    su-tools nobody -s /bin/bash -c "make tests"
    make install || exit -1
    exec /bin/bash --login +h
}

# 6.30 Libtool-2.4
step0630() {
    cd $LFS/sources
    cd libtool-2.4 || exit -1

    ./configure --prefix=/usr || exit -1
    make && make check && make install || exit -1

}

# 6.31 GDBM-1.8.3
step0631() {
    cd $LFS/sources
    cd gdbm-1.8.3 || exit -1

    ./configure --prefix=/usr
    make && make install make install-compat
    install-info --dir-file=/usr/info/dir /usr/info/gdbm.info
}

# 6.32 Inetutils-1.8
step0632() {
    cd $LFS/sources
    cd inetutils-1.8 || exit -1

    ./configure --prefix=/usr --libexecdir=/usr/sbin \
        --localstatedir=/var --disable-ifconfig \
        --disable-logger --disable-syslogd --disable-whois \
        --disable-servers
    make && make check && make install || exit -1
    make -C doc html
    make -C doc install-html docdir=/usr/share/doc/inetutils-1.8
}

# 6.33 Perl-5.12.3
step0633() {
    cd $LFS/sources
    cd perl-5.12.3 || exit -1

    echo "127.0.0.1 localhost $(hostname)" > /etc/hosts
    sed -i -e "s|BUILD_ZLIB\s*= True|BUILD_ZLIB = False|" \
        -e "s|INCLUDE\s*= ./zlib-src|INCLUDE = /usr/include|" \
        -e "s|LIB\s*= ./zlib-src|LIB = /usr/lib|" \
        cpan/Compress-Raw-Zlib/config.in
    sh Configure -des -Dprefix=/usr \
        -Dvendorprefix=/usr \
        -Dman1dir=/usr/share/man/man1 \
        -Dman3dir=/usr/share/man/man3 \
        -Dpager="/usr/bin/less -isR" \
        -Duseshrplib
    make && make test && make install || exit -1
}

# 6.34 Autoconf-2.68
step0634() {
    cd $LFS/sources
    cd autoconf-2.68 || exit -1

    ./configure --prefix=/usr || exit -1
    make && make check && make install || exit -1
}

# 6.35 Automake-1.11.1
step0635() {
    cd $LFS/sources
    cd automake-1.11.1 || exit -1

    ./configure --prefix=/usr --docdir=/usr/share/doc/automake-1.11.1 || exit -1
    make && make check && make install || exit -1
}

# 6.36 Bzip2-1.0.6
step0636() {
    cd $LFS/sources
    cd bzip2-1.0.6 || exit -1

    patch -Np1 -i ../bzip2-1.0.6-install_docs-1.patch
    sed -i 's@\(ln -s -f \)$(PREFIX)/bin/@\1@' Makefile
    make -f Makefile-libbz2_so && make clean && make && make PREFIX=/usr intall || exit -1

    cp -v bzip2-shared /bin/bzip2
    cp -av libbz2.so* /lib
    ln -sv ../../lib/libbz2.so.1.0 /usr/lib/libbz2.so
    rm -v /usr/bin/{bunzip2,bzcat,bzip2}
    ln -sv bzip2 /bin/bunzip2
    ln -sv bzip2 /bin/bzcat
}

# 6.37 Diffutils-3.0
step0637() {
    cd $LFS/sources
    if [ -d diffutils-3.0 ];then
        rm -rf diffutils-3.0
    fi
    tar -xzf diffutils-3.0.tar.gz || exit -1
    cd diffutils-3.0

    ./configure --prefix=/usr || exit -1
    make && make check && make install || exit -1
}

# 6.38 Gawk-3.1.8
step0638() {
    cd $LFS/sources
    if [ -d gawk-3.1.8 ];then
        rm -rf gawk-3.1.8
    fi
    tar -xjf gawk-3.1.8.tar.bz2 || exit -1
    cd gawk-3.1.8

    ./configure --prefix=/usr --libexecdir=/usr/lib
    make && make check && make install || exit -1
    mkdir -v /usr/share/doc/gawk-3.1.8
    cp -v doc/{awkforai.txt,*.{eps,pdf,jpg}} \
        /usr/share/doc/gawk-3.1.8
}


# 6.39 File-5.05
step0639() {
    cd $LFS/sources
    if [ -d file-5.05 ];then
        rm -rf file-5.05
    fi
    tar -xzf file-5.05.tar.gz || exit -1
    cd file-5.05

    ./configure --prefix=/usr || exit -1
    make && make check && make install || exit -1
}

# 6.40 Findutils-4.4.2
step0640() {
    cd $LFS/sources
    cd findutils-4.4.2

    ./configure --prefix=/usr --libexecdir=/usr/lib/findutils \
        --localstatedir=/var/lib/locate || exit -1
    make && make check && make install || exit -1

    mv -v /usr/bin/find /bin
    sed -i 's/find:=${BINDIR}/find:=\/bin/' /usr/bin/updatedb
}

# 6.41 Flex-2.5.35
step0641() {
    cd $LFS/sources
    if [ -d flex-2.5.35 ];then
        rm -rf flex-2.5.35
    fi
    tar -xjf flex-2.5.35.tar.bz2 || exit -1
    cd flex-2.5.35

    patch -Np1 -i ../flex-2.5.35-gcc44-1.patch
    ./configure --prefix=/usr || exit -1
    make && make check && make install || exit -1

    ln -sv libfl.a /usr/lib/libl.a

    cat > /usr/bin/lex << "EOF"
#!/bin/sh
# Begin /usr/bin/lex
exec /usr/bin/flex -l "$@"
# End /usr/bin/lex
EOF
    chmod -v 755 /usr/bin/lex
    mkdir -v /usr/share/doc/flex-2.5.35
    cp -v doc/flex.pdf /usr/share/doc/flex-2.5.35
}

# 6.42 Gettext-0.18.1.1
step0642() {
    cd $LFS/sources
    if [ -d gettext-0.18.1.1 ];then
        rm -rf gettext-0.18.1.1
    fi
    tar -xzf gettext-0.18.1.1.tar.gz || exit -1
    cd gettext-0.18.1.1

    ./configure --prefix=/usr --docdir=/usr/share/doc/gettext-0.18.1.1
    make && make check && make install || exit -1
}

# 6.43 Groff-1.21
step0643() {
    cd $LFS/sources
    #if [ -d groff-1.21 ];then
    #    rm -rf groff-1.21
    #fi
    #tar -xzf groff-1.21.tar.gz || exit -1
    cd groff-1.21

    PAGE=A4 ./configure --prefix=/usr || exit -1
    make && make install || exit -1

    ln -sv eqn /usr/bin/geqn
    ln -sv tbl /usr/bin/gtbl
}

# 6.44 GRUB-1.98
step0644() {
    cd $LFS/sources
    cd grub-1.98

    ./configure --prefix=/usr \
        --sysconfdir=/etc \
        --disable-grub-emu-usb \
        --disable-grub-fstest \
        --disable-efiemu || exit -1
    make && make install || exit -1
}

# 6.45 Gzip-1.4
step0645() {
    cd $LFS/sources
    cd gzip-1.4

    ./configure --prefix=/usr --bindir=/bin || exit -1
    make && make check && make install || exit -1
    mv -v /bin/{gzexe,uncompress,zcmp,zdiff,zegrep} /usr/bin
    mv -v /bin/{zfgrep,zforce,zgrep,zless,zmore,znew} /usr/bin
}

# 6.46 IPRoute2-2.6.37
step0646() {
    cd $LFS/sources
    cd iproute2-2.6.37

    sed -i '/^TARGETS/s@arpd@@g' misc/Makefile
    sed -i '1289i\\tfilter.cloned = 2;' ip/iproute.c
    make DESTDIR= || exit -1
    make DESTDIR= SBINDIR=/sbin MANDIR=/usr/share/man \
        DOCDIR=/usr/share/doc/iproute2-2.6.37 install || exit -1
}

# 6.47 Kbd-1.15.2
step0647() {
    cd $LFS/sources
    cd kbd-1.15.2

    patch -Np1 -i ../kbd-1.15.2-backspace-1.patch

    ./configure --prefix=/usr --datadir=/lib/kbd || exit -1
    make && make install || exit -1

    mv -v /usr/bin/{kbd_mode,loadkeys,openvt,setfont} /bin
    mkdir -v /usr/share/doc/kbd-1.15.2
    cp -R -v doc/* /usr/share/doc/kbd-1.15.2
}

# 6.48 Less-436
step0648() {
    cd $LFS/sources
    cd less-436

    ./configure --prefix=/usr --sysconfdir=/etc || exit -1
    make && make install || exit -1
}

# 6.49 Make-3.82
step0649() {
    cd $LFS/sources
    cd make-3.82

    ./configure --prefix=/usr || exit -1
    make && make check && make install || exit -1
}

# 6.50 Xz-5.0.1
step0650() {
    cd $LFS/sources
    cd xz-5.0.1

    ./configure --prefix=/usr --docdir=/usr/share/doc/xz-5.0.1 || exit -1
    make && make check && make install || exit -1
}

# 6.51 Man-DB-2.5.9
step0651() {
    cd $LFS/sources
    cd man-db-2.5.9

    ./configure --prefix=/usr --libexecdir=/usr/lib \
        --docdir=/usr/share/doc/man-db-2.5.9 --sysconfdir=/etc --disable-setuid \
        --with-browser=/usr/bin/lynx --with-vgrind=/usr/bin/vgrind \
        --with-grap=/usr/bin/grap || exit -1
    make && make -k check && make install || exit -1
}

# 6.52 Module-Init-Tools-3.12
step0652() {
    cd $LFS/sources
    cd module-init-tools-3.12

    echo '.so man5/modprobe.conf.5' > modprobe.d.5
    ./configure && make check || exit -1
    ./tests/runtests && make clean || exit -1
    ./configure --prefix=/ --enable-zlib-dynamic --mandir=/usr/share/man || exit -1
    make && make INSTALL=install install || exit -1
}

# 6.53 Patch-2.6.1
step0653() {
    cd $LFS/sources
    cd patch-2.6.1

    patch -Np1 -i ../patch-2.6.1-test_fix-1.patch

    ./configure --prefix=/usr || exit -1
    make && make check && make install || exit -1
}

# 6.54 Psmisc-22.13
step0654() {
    cd $LFS/sources
    cd psmisc-22.13

    ./configure --prefix=/usr || exit -1
    make && make install || exit -1

    mv -v /usr/bin/fuser /bin
    mv -v /usr/bin/killall /bin
}

# 6.55 Shadow-4.1.4.3
step0655() {
    cd $LFS/sources
    cd shadow-4.1.4.3

    sed -i 's/groups$(EXEEXT) //' src/Makefile.in
    find man -name Makefile.in -exec sed -i 's/groups\.1 / /' {} \;
    sed -i 's/man_MANS = $(man_nopam) /man_MANS = /' man/ru/Makefile.in
    sed -i -e 's@#ENCRYPT_METHOD DES@ENCRYPT_METHOD SHA512@' \
        -e 's@/var/spool/mail@/var/mail@' etc/login.defs
    sed -i 's@DICTPATH.*@DICTPATH\t/lib/cracklib/pw_dict@' etc/login.defs
    ./configure --sysconfdir=/etc || exit -1
    make && make install || exit -1
    mv -v /usr/bin/passwd /bin

    pwconv
    grpconv
    sed -i 's/yes/no/' /etc/default/useradd
    passwd root
}

# 6.56 Sysklogd-1.5
step0656() {
    cd $LFS/sources
    cd sysklogd-1.5

    make && make BINDIR=/sbin install || exit -1

    cat > /etc/syslog.conf << "EOF"
# Begin /etc/syslog.conf
auth,authpriv.* -/var/log/auth.log
*.*;auth,authpriv.none -/var/log/sys.log
daemon.* -/var/log/daemon.log
kern.* -/var/log/kern.log
mail.* -/var/log/mail.log
user.* -/var/log/user.log
*.emerg *
# End /etc/syslog.conf
EOF
}

# 6.57 Sysvinit-2.88dsf
step0657() {
    cd $LFS/sources
    cd sysvinit-2.88dsf

    sed -i 's@Sending processes@& configured via /etc/inittab@g' \
        src/init.c
    sed -i -e 's/utmpdump wall/utmpdump/' \
        -e 's/mountpoint.1 wall.1/mountpoint.1/' src/Makefile
    make -C src && make -C src install || exit -1

    cat > /etc/inittab << "EOF"
# Begin /etc/inittab
id:3:initdefault:
si::sysinit:/etc/rc.d/init.d/rc sysinit
l0:0:wait:/etc/rc.d/init.d/rc 0
l1:S1:wait:/etc/rc.d/init.d/rc 1
l2:2:wait:/etc/rc.d/init.d/rc 2
l3:3:wait:/etc/rc.d/init.d/rc 3
l4:4:wait:/etc/rc.d/init.d/rc 4
l5:5:wait:/etc/rc.d/init.d/rc 5
l6:6:wait:/etc/rc.d/init.d/rc 6
ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now
su:S016:once:/sbin/sulogin
1:2345:respawn:/sbin/agetty tty1 9600
2:2345:respawn:/sbin/agetty tty2 9600
3:2345:respawn:/sbin/agetty tty3 9600
4:2345:respawn:/sbin/agetty tty4 9600
5:2345:respawn:/sbin/agetty tty5 9600
6:2345:respawn:/sbin/agetty tty6 9600
# End /etc/inittab
EOF
}

# 6.58 Tar-1.25
step0658() {
    cd /sources/tar-1.25

    FORCE_UNSAFE_CONFIGURE=1 ./configure --prefix=/usr \
        --bindir=/bin --libexecdir=/usr/sbin || exit -1
    make && make check && make install || exit -1
    make -C doc install-html docdir=/usr/share/doc/tar-1.25 || exit -1
}

# 6.59 Texinfo-4.13a
step0659() {
    cd /sources/texinfo-4.13a

    ./configure --prefix=/usr || exit -1
    make && make check && make install || exit -1
    make TEXMF=/usr/share/texmf install-tex || exit -1

    cd /usr/share/info
    rm -v dir
    for f in *
        do install-info $f dir 2>/dev/null
    done
}

# 6.60 Udev-166
step0660() {
    cd /sources/udev-166

    tar -xjf ../udev-config-20100128.tar.bz2
    tar -xvf ../udev-166-testfiles.tar.bz2 --strip-components=1

    install -dv /lib/{firmware,udev/devices/{pts,shm}}
    mknod -m0666 /lib/udev/devices/null c 1 3
    ./configure --prefix=/usr --sysconfdir=/etc --sbindir=/sbin \
        --with-rootlibdir=/lib --libexecdir=/lib/udev \
        --disable-extras --disable-introspection
    make && make check && make install || exit -1

    rmdir -v /usr/share/doc/udev
    cd udev-config-20100128
    make install && make install-doc || exit -1
}

# 6.61 Vim-7.3
step0661() {
    cd /sources/vim-7.3

    echo '#define SYS_VIMRC_FILE "/etc/vimrc"' >> src/feature.h

    ./configure --prefix=/usr --enable-multibyte || exit -1
    make && make test && make install || exit -1

    ln -sv vim /usr/bin/vi
    for L in /usr/share/man/{,*/}man1/vim.1; do
        ln -sv vim.1 $(dirname $L)/vi.1
    done
    ln -sv ../vim/vim73/doc /usr/share/doc/vim-7.3
    cat > /etc/vimrc << "EOF"
" Begin /etc/vimrc
set nocompatible
set backspace=2
syntax on
if (&term == "iterm") || (&term == "putty")
set background=dark
endif
" End /etc/vimrc
EOF
}

# 6.63 Stripping Again
step0663() {
    cd /

    logout

    chroot $LFS /tools/bin/env -i \
        HOME=/root TERM=$TERM PS1='\u:\w\$ ' \
        PATH=/bin:/usr/bin:/sbin:/usr/sbin \
    /tools/bin/bash --login
        /tools/bin/find /{,usr/}{bin,lib,sbin} -type f \
        -exec /tools/bin/strip --strip-debug '{}' ';'
}

# 6.64 Cleaning Up
step0664() {
    chroot "$LFS" /usr/bin/env -i \
        HOME=/root TERM="$TERM" PS1='\u:\w\$ ' \
        PATH=/bin:/usr/bin:/sbin:/usr/sbin \
    /bin/bash --login
}

# 7.2 LFS-Bootscripts-20100627
step0702() {
    cd /sources/lfs-bootscripts-20100627

    make install || exit -1
}

# 7.4 Configuring
step0704() {
    cat > /etc/sysconfig/clock << "EOF"
# Begin /etc/sysconfig/clock
UTC=1
# Set this to any options you might need to give to hwclock,
# such as machine hardware clock type for Alphas.
CLOCKPARAMS=
# End /etc/sysconfig/clock
EOF
    cat > /etc/sysconfig/console << "EOF"
# Begin /etc/sysconfig/console
UNICODE="1"
KEYMAP="latin1"
FONT="lat0-16 -m 8859-15"
# End /etc/sysconfig/console
EOF
cat > /etc/inputrc << "EOF"
# Begin /etc/inputrc
# Modified by Chris Lynn <roryo@roryo.dynup.net>
# Allow the command prompt to wrap to the next line
set horizontal-scroll-mode Off
# Enable 8bit input
set meta-flag On
set input-meta On
# Turns off 8th bit stripping
set convert-meta Off
# Keep the 8th bit for display
set output-meta On
# none, visible or audible
set bell-style none
# All of the following map the escape sequence of the value
# contained in the 1st argument to the readline specific functions
"\eOd": backward-word
"\eOc": forward-word
# for linux console
"\e[1~": beginning-of-line
"\e[4~": end-of-line
"\e[5~": beginning-of-history
"\e[6~": end-of-history
"\e[3~": delete-char
"\e[2~": quoted-insert
# for xterm
"\eOH": beginning-of-line
"\eOF": end-of-line
# for Konsole
"\e[H": beginning-of-line
"\e[F": end-of-line
# End /etc/inputrc
EOF

# I'm not clear about locale, confusing here!!! Skipped!
LC_ALL=C name> locale charmap
#LC_ALL=<locale name> locale language
#LC_ALL=<locale name> locale int_curr_symbol
#LC_ALL=<locale name> locale int_prefix

    cat > /etc/profile << "EOF"
# Begin /etc/profile
export LANG=<ll>_<CC>.<charmap><@modifiers>
# End /etc/profile
EOF

    cat > /etc/udev/rules.d/83-duplicate_devs.rules << "EOF"
# Persistent symlinks for webcam and tuner
KERNEL=="video*", ATTRS{idProduct}=="1910", ATTRS{idVendor}=="0d81", \
SYMLINK+="webcam"
KERNEL=="video*", ATTRS{device}=="0x036f", ATTRS{vendor}=="0x109e", \
SYMLINK+="tvtuner"
EOF

    echo "HOSTNAME=<lfs>" > /etc/sysconfig/network

    cat > /etc/hosts << "EOF"
# Begin /etc/hosts (network card version)
127.0.0.1 localhost
# End /etc/hosts (network card version)
EOF

# 7.13 Configuring the network Script
    cd /etc/sysconfig/network-devices
    mkdir -v ifconfig.eth0
    cat > ifconfig.eth0/ipv4 << "EOF"
ONBOOT=yes
SERVICE=ipv4-static
IP=192.168.1.1
GATEWAY=192.168.1.2
PREFIX=24
BROADCAST=192.168.1.255
EOF

    cat > /etc/resolv.conf << "EOF"
# Begin /etc/resolv.conf
domain cisco.com
nameserver 8.8.8.8
# End /etc/resolv.conf
EOF

    cat > /etc/fstab << "EOF"
# Begin /etc/fstab
# file system mount-point type options dump fsck
# order
UUID=e9f3e7aa-a78c-4d7c-acbe-bff3a5fa56d7 /                       ext4    defaults        1 1
UUID=b3109664-d111-4dd1-9ddc-d07c438944e2 swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
# End /etc/fstab
EOF

}

# 8.3 Linux-2.6.37
step0803() {
    cd /sources/linux-2.6.37
    make mrproper
    make LANG=en LC_ALL= menuconfig
    make && make modules_install

    cp -v arch/x86/boot/bzImage /boot/vmlinux-2.6.37-lfs-6.8
    cp -v System.map /boot/System.map-2.6.37
    cp -v .config /boot/config-2.6.37
    install -d /usr/share/doc/linux-2.6.37
    cp -r Documentation/* /usr/share/doc/linux-2.6.37

    install -v -m755 -d /etc/modprobe.d
    cat > /etc/modprobe.d/usb.conf << "EOF"
# Begin /etc/modprobe.d/usb.conf
install ohci_hcd /sbin/modprobe ehci_hcd ; /sbin/modprobe -i ohci_hcd ; true
install uhci_hcd /sbin/modprobe ehci_hcd ; /sbin/modprobe -i uhci_hcd ; true
# End /etc/modprobe.d/usb.conf
EOF
}

# 8.4 Using GRUB to Set Up the Boot Process
step0804() {
    grub-install --grub-setup=/bin/true /dev/sda
    grub-mkconfig -o /boot/grub/grub.cfg
}

# Rebooting the System
step0903() {
    logout
    umount -v $LFS/dev/pts
    umount -v $LFS/dev/shm
    umount -v $LFS/dev
    umount -v $LFS/proc
    umount -v $LFS/sys
    umount -v $LFS
    umount -v $LFS/usr
    umount -v $LFS/home
    umount -v $LFS
    shutdown -r now
}

set -v

#step0510
#step0511
#step0512
#step0513
#step0514
#step0515
#step0516
#step0517
#step0518
#step0519
#step0520
#step0521
#step0522
#step0523
#step0524
#step0525
#step0526
#step0527
#step0528
#step0529
#step0530
#step0531
#step0532
#step0533

#==Execute step0534 manually
#==Execute step0602 manually
#==Execute step0604 manually
#==Execute step0605 manually
#==Execute step0606 manually

export LFS=/.
set -v

#step0607
#step0608
step0609

step0610
step0611
step0612
step0613
step0614
step0615
step0616
step0617
step0618
step0619
step0620
step0621
step0622
step0623
step0624
step0625
step0626
step0627
step0628
step0629
step0630


# Execute step0663 manually
# Execute step0664 manually


#echo 6.8 > /etc/lfs-release
exit 0

